home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Audio / Patchmix / Source / Comb.m < prev    next >
Text File  |  1992-08-18  |  5KB  |  232 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Comb.h"
  5. #import "Instrum.h"
  6. #import <appkit/graphics.h>
  7. #import "Statement.h"
  8.  
  9. @implementation Comb
  10.  
  11. + initialize
  12. {
  13.        image = [NXImage findImageNamed:"Comb"];
  14.     rvtOffset.x = 3;
  15.     rvtOffset.y = 30;
  16.     inOffset.x = 30;
  17.     inOffset.y = 51;
  18.     pchOffset.x = 52;
  19.     pchOffset.y = 30;
  20.     outOffset.x = 29;
  21.     outOffset.y = 3;
  22.     
  23.     return self;
  24. }
  25.  
  26. - init
  27. {
  28.     written = NO;
  29.     size.width = 80.;
  30.     size.height = 80.;
  31.     center.x = size.width/2.;
  32.     center.y = size.height/2.;
  33.     strcpy(type,"Comb");
  34.     strcpy(name,"comb");    
  35.     
  36.     rvt = [[Param alloc] init:self :&rvtOffset];
  37.     in = [[Param alloc] init:self :&inOffset];
  38.     pch = [[Param alloc] init:self :&pchOffset];
  39.     out = [[Param alloc] init:self :&outOffset];
  40.  
  41.     [rvt setValue:".5"];    // set non-"1" defaupch values
  42.     [pch setValue:"440"];    
  43.     [rvt setTitle:"RvbTi:"];
  44.     [pch setTitle:"LpTi"];
  45.     [in setTitle:"Input:"];
  46.     [out setTitle:"Out:"];
  47.         
  48.     paramList = [[List alloc] initCount:5];
  49.     [paramList addObject:rvt];
  50.     [paramList addObject:in];
  51.     [paramList addObject:pch];
  52.     [paramList addObject:out];
  53.     
  54.     [Inst putUgenInList:self];
  55.     
  56.     return self;
  57. }
  58.  
  59. - remove
  60. {
  61.     id cp,param;
  62.     int i;
  63.                             // make sure not connected first 
  64.     for(i = 0; i < ([paramList count]); i++) {    
  65.         param = [paramList objectAt:i];
  66.         if(cp = [param getConnectedParam]) 
  67.             return 0;
  68.     }
  69.                             // remove ugen from list
  70.     [Inst removeUgenFromList:self];
  71.     return self;
  72. }
  73.  
  74. - (NXImage *)getImage
  75. {
  76.     return image;
  77. }
  78.  
  79. - move:(NXPoint *)newloc
  80. {
  81.     location = *newloc;
  82.     [rvt move:&location];
  83.     [in move:&location];
  84.     [pch move:&location];
  85.     [out move:&location];
  86.     return self;
  87. }
  88.  
  89. - findParamAtPoint:(NXPoint *)point
  90. {
  91.     NXRect *rect;
  92.     int i;
  93.     id param;
  94.     
  95.     for(i = 0; i < [paramList count]; i++) {
  96.         param = [paramList objectAt:i];
  97.         rect = [param getRect];
  98.         if(NXMouseInRect(point,rect,NO))
  99.             return param;
  100.     }
  101.     return nil;
  102. }
  103.  
  104. - writeUgen
  105. {
  106.     BOOL ai = NO;    // amplitude input ugen
  107.     char ampVar[10];
  108.     char par[20];
  109.     /*     for params:
  110.             see if there is input ugen
  111.                 if so, grab the output (ug?) and mupchiply it by this
  112.                     param value
  113.                 if not just use the param value
  114.          after all params done, write relevant code into lists
  115.             (declarations, assignments and loop statements)
  116.             i.e. ug? = oscil(,,,,);
  117.         go to output param connected ugen and call its "writeUgen"
  118.     */
  119.     int i;
  120.     int parent[4];
  121.     id param;
  122.     id ug;
  123.     id nupar;
  124.     char str[50];
  125.     
  126. //    printf("writing ugen %d\n",index);
  127.     sprintf(str,"\tfloat ug%d;\n",index);
  128.     [Inst putVarInList:str];
  129.     sprintf(str,"\tfloat amp%d;\n",index);
  130.     [Inst putVarInList:str];
  131.     sprintf(str,"\tfloat rvt%d;        // reverb time\n",index);
  132.     [Inst putVarInList:str];
  133.     sprintf(str,"\tfloat in%d;\n",index);
  134.     [Inst putVarInList:str];
  135.     sprintf(str,"\tfloat pch%d;\n",index);
  136.     [Inst putVarInList:str];
  137.     sprintf(str,"\tfloat out%d;\n",index);
  138.     [Inst putVarInList:str];
  139.     sprintf(str,"\tfloat loopt%d;         // loop time\n",index);
  140.     [Inst putVarInList:str];
  141.     sprintf(str,"\tfloat combarr%d[10000];\n",index);
  142.     [Inst putVarInList:str];
  143.  
  144.     for(i = 0; i < ([paramList count]-1); i++) {    
  145.         param = [paramList objectAt:i];
  146.         if(nupar = [param getConnectedParam]) {
  147.             ug = [nupar getUgen];
  148.             if([ug getWritten] == NO) { 
  149.                 parent[i] = [[ug writeUgen] getIndex];
  150.             }
  151.             else  {
  152.                 parent[i] = 0;
  153.             }
  154.         }
  155.         else 
  156.             parent[i] = 0;
  157.         
  158.     }
  159.  
  160.                                 // reverb time
  161.     strcpy(par,[[paramList objectAt:0] getValue]);
  162.     if(par[0] == 'p' && par[1] == '[')
  163.         sprintf(str,"\trvt%d = %s;\n", index, par);
  164.     else
  165.         sprintf(str,"\trvt%d = %.2f;\n", index, atof(par));
  166.     [Inst putAssignInList:str];
  167.     
  168.                                 // input sig
  169.     strcpy(par,[[paramList objectAt:1] getValue]);
  170.     if(par[0] == 'p' && par[1] == '[')
  171.         sprintf(str,"\tamp%d = %s;\n", index, par);
  172.     else
  173.         sprintf(str,"\tamp%d = %.2f;\n", index, atof(par));
  174.     [Inst putAssignInList:str];
  175.     
  176.                                 // pitch
  177.     strcpy(par,[[paramList objectAt:2] getValue]);
  178.     if(par[0] == 'p' && par[1] == '[')
  179.         sprintf(str,"\tloopt%d = 1/%s;\n", index, par);
  180.     else
  181.         sprintf(str,"\tloopt%d = 1/%.2f;\n", index, atof(par));
  182.     [Inst putAssignInList:str];
  183.  
  184.     if(parent[1]) {     //  input ugen    
  185.         ai = YES;
  186.         sprintf(str,"\tin%d = %.2f;\n",index, 
  187.             atof([[paramList objectAt:1] getValue]));
  188.         [Inst putAssignInList:str];
  189.         sprintf(str,"\tfloat inai%d;\n",index);
  190.         [Inst putVarInList:str];
  191.         sprintf(str,"\t\tinai%d = amp%d * ug%d;\n",index,  
  192.             index,parent[1]);
  193.         [Inst putLoopInList:str];
  194.     }
  195.     else {                // input preset or default
  196.          strcpy(par,[[paramList objectAt:0] getValue]);
  197.         if(par[0] == 'p' && par[1] == '[')
  198.             sprintf(str,"\tamp%d = %s;\n", index, par);
  199.         else
  200.             sprintf(str,"\tamp%d = %.2f;\n",index,atof(par));
  201.         [Inst putAssignInList:str];
  202.     }
  203.     if(ai)
  204.         sprintf(ampVar,"inai%d",index);
  205.     else
  206.         sprintf(ampVar,"amp%d",index);
  207.         
  208.     sprintf(str,"\tnsamps = nsamps + (rvt%d * SR);   // add reverbtime to out duration\n",  index, index, index);
  209.     [Inst putAssignInList:str];
  210.          
  211.                         //     set up comb filter
  212.     sprintf(str, "\tcombset(loopt%d,rvt%d,0,combarr%d);\n",index,index, index);
  213.     [Inst putAssignInList:str];
  214.     
  215.                         //     output amplitude multiplier:
  216.     strcpy(par,[[paramList objectAt:3] getValue]);
  217.     if(par[0] == 'p' && par[1] == '[')
  218.         sprintf(str,"\tout%d = %s;\n",index,par);
  219.     else
  220.         sprintf(str,"\tout%d = %.2f;\n", index, atof([[paramList objectAt:3] getValue]));
  221.     [Inst putAssignInList:str];
  222.  
  223.     sprintf(str,"\t\tug%d = %s(%s,combarr%d) * out%d;\n", index, name, ampVar, index, index);
  224.     [Inst putLoopInList:str];
  225.     
  226.     written = YES;
  227.     return self;    
  228. }
  229.  
  230.  
  231. @end
  232.